package com.gopay.bis.pab.ebank.operation;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.pab.ebank.PadQueryOrdersMessageReq;
import com.gopay.common.domain.bank.pab.ebank.PabOrderMessageRes;
import com.gopay.common.domain.bank.pab.ebank.PabQueryOrdersMessageRes;
import com.sdb.ebank.bean.data.AllOrderListReceive;
import com.sdb.ebank.bean.data.AllOrderListSend;

/**
 * 深发展订单查询返回处理类
 * 
 * @SDBTransformer.java
 * @author WangHongFei
 */
public class PabTransformer {

	public AllOrderListSend format(PadQueryOrdersMessageReq req) {
		Date begin = req.getBeginDate();
		Date end = req.getEndDate();
		String begintime = req.getBeginTime();
		String endtime = req.getEndTime();
		String orderId = req.getOrderId();
		return new AllOrderListSend(begin, end, begintime, endtime, orderId);
	}

	public PabQueryOrdersMessageRes parse(AllOrderListReceive receive) {
		PabQueryOrdersMessageRes res = new PabQueryOrdersMessageRes();
		res.setOutStlmCd(BankConstants.BANK_LOGO_PAB);
		res.setStatus(receive.getStatus());
		res.setErrDesc(receive.getErrDesc());
		res.setCount(receive.getCount());
		res.setSum(receive.getSum());
		res.setOrders(getGopayOrders(receive.getOrders()));
		return res;
	}

	/**
	 * 根据银行返回数据集，构造平台使用的vo数据集
	 * 
	 * 银行返回数据集以map形式存储 如下： ArrayList --HashMap ----orderId="" ----amount=""
	 * ----merchantDate="" ----payDate="" ----commitDate="" ----seqNo=""
	 * ----commitFlag="" 平台 使用实体对象形式存储返回结果集 如下： ArrayList --ResSDBOrderMessage
	 * ----orderId/get、set ----amount/get、set ----merchantDate/get、set
	 * ----payDate/get、set ----commitDate/get、set ----seqNo/get、set
	 * ----commitFlag/get、set
	 * 
	 * @param orders
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private ArrayList<Object> getGopayOrders(ArrayList<Object> orders) {
		ArrayList<Object> gopayOrders = new ArrayList<Object>();
		for (int i = 0; i < orders.size(); i++) {
			gopayOrders.add(getResSDBOrderMessage((HashMap) orders.get(i)));
		}
		return gopayOrders;
	}

	/**
	 * 获得单笔订单信息
	 * 
	 * @param map
	 * @return
	 */
	private PabOrderMessageRes getResSDBOrderMessage(HashMap map) {
		PabOrderMessageRes order = new PabOrderMessageRes();
		BeanWrapper bw = new BeanWrapperImpl(order);
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = map.get(key);
			bw.setPropertyValue(key, value);
		}
		return order;
	}
}
